<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="primary" @click="newLegal">新增法制宣传</el-button>
    </div>
    <div class="card-header">法制宣传管理</div>
    <el-card class="box-card ">
      <el-card class="box-card">
        <el-form ref="searchLegalCondition" :model="searchLegalCondition" label-width="80px" inline>
          <el-form-item label="标题">
            <el-input v-model="searchLegalCondition.title" style="width: 300px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchLegalCondition.status" placeholder="请选择">
              <el-option
                v-for="item in LegalStatus"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search(1)">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <div class="flex-row">
        <div class="flex-item">
          <el-card class="box-card">
            <el-table
              :data="legalList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>
              <el-table-column prop="image" label="图片" width="210">
                <template slot-scope="scope">
                  <image-cover :thumb.sync="scope.row.image" width="187.5px" height="125px" size-type="contain"></image-cover>
                </template>
              </el-table-column>
              <el-table-column prop="title" label="标题" ></el-table-column>
              <el-table-column
                prop="provider"
                label="发布部门" width="120">
              </el-table-column>
              <el-table-column
                prop="status"
                label="状态" width="120">
                <template slot-scope="scope">
                  <span>{{scope.row.status| keyText('LegalStatus')}}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="220">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" @click="preLegal(scope.row)">
                    预览
                  </el-button>
                  <el-button size="mini" v-if="scope.row.status===3||scope.row.status===2||scope.row.status===1" @click="editLegal(scope.$index, scope.row)" type="text">编辑</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===3" @click="save(scope.row,2)">提交审核</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===3" v-has="{class:'legal:publish'}" @click="save(scope.row,3)">发布</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===4" v-has="{class:'legal:reject'}" @click="save(scope.row,5)">驳回</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===4" v-has="{class:'legal:pass'}" @click="save(scope.row,4)">通过</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===1" v-has="{class:'legal:up'}" @click="save(scope.row,7)">上架</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===5" v-has="{class:'legal:down'}" @click="save(scope.row,6)">下架</el-button>

                  <el-button size="mini" type="text" @click="deleteLegal(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="legalList.totalElements"
                :page-size="legalList.size"
                :current-page.sync="currentLegalPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
        </div>
      </div>
    </el-card>

    <el-dialog :visible.sync="showLegalEditor" title="添加/编辑 法制宣传" width="80%">
      <legal-editor @close="onLegalEditorClose" :is-close="showLegalEditor"></legal-editor>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="showPreview" width="50%">
      <pre-legal @close="onShowPreviewClose"  :is-close="showPreview"></pre-legal>
    </el-dialog>


  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import LegalEditor from "./legal-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import PreLegal from "./pre-legal";
import Status from "@/libs/status";
export default {
  created() {
    this.$store.dispatch("getLegalPageS").then(ret => {});
  },
  mounted() {},
  data() {
    return {
      currentLegalPage: 1,
      showLegalEditor: false,
      showPreview: false,
      curPreObj: {},
      LegalStatus: Status.LegalStatus
    };
  },
  computed: {
    ...mapGetters({
      legalList: "legalList",
      searchLegalCondition: "searchLegalCondition"
    })
  },
  methods: {
    getPageChange(val) {
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchLegalCondition
      );
      this.$store.dispatch("getLegalPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(1);
    },
    deleteLegal(row) {
      this.$confirm("确认删除当前法制宣传?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteLegalS", { id: row.id }).then(ret => {
            this.getPageChange(1);
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    preLegal(row) {
      this.$store.dispatch("getLegalS", row.id).then(ret => {});
      this.showPreview = true;
    },
    newLegal() {
      this.$store.commit("INIT_CURRENT_LEGAL");
      this.showLegalEditor = true;
    },
    editLegal(index, row) {
      this.$store.dispatch("getLegalS", row.id).then(ret => {});
      this.showLegalEditor = true;
    },
    onLegalEditorClose() {
      this.showLegalEditor = false;
      this.getPageChange(1);
    },
    save(row, status) {
      row.opt = status;
      this.$store.dispatch("addOrUpdateLegalS", row).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(1);
      });
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(1);
    }
  },
  components: {
    PageLayout,
    LegalEditor,
    ImageCover,
    PreLegal
  }
};
</script>
<style>
</style>
